<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择类别">
          <el-option v-for="dict in dict.type.moe_message_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-has-permi="['system:message:edit']" :disabled="multiple" icon="el-icon-plus" plain size="mini"
          type="primary" @click="signRead">已读
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-has-permi="['system:message:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
          type="danger" @click="handleDelete">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-has-permi="['system:message:export']" icon="el-icon-download" plain size="mini" type="warning"
          @click="handleExport">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="消息ID" prop="id" />
      <el-table-column align="center" label="发送者" prop="senderId">
        <template slot-scope="scope">
          {{ scope.row.senderName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="接收者" prop="receiverId">
        <template slot-scope="scope">
          {{ scope.row.receiverName }}
        </template>

      </el-table-column>
      <el-table-column align="center" label="标题" prop="title" />
      <el-table-column align="center" label="类别" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.moe_message_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否已读" prop="readStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.moe_message_status" :value="scope.row.readStatus" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="阅读时间" prop="readTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.readTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-has-permi="['system:message:query']" icon="el-icon-view" size="mini" type="text"
            @click="handleGetMessage(scope.row)">详情
          </el-button>
          <el-button v-has-permi="['system:message:remove']" icon="el-icon-delete" size="mini" type="text"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
      @pagination="getList" />

    <!-- 添加或修改消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" :disabled="true" />
        </el-form-item>
        <el-form-item label="发送者" prop="senderId">
          <el-input v-model="form.senderName" placeholder="请选择发送者" :disabled="true" />
        </el-form-item>
        <el-form-item label="接收者" prop="receiverId">
          <el-input v-model="form.receiverName" placeholder="请选择接收者" :disabled="true" />
        </el-form-item>
        <el-form-item label="消息体">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="类别" prop="type">
          <el-select v-model="form.type" placeholder="请选择类别" :disabled="true">
            <el-option v-for="dict in dict.type.moe_message_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否已读" prop="readStatus">
          <el-select v-model="form.readStatus" placeholder="请选择是否已读" :disabled="true">
            <el-option v-for="dict in dict.type.moe_message_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目经理" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择项目经理">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择类别">
            <el-option v-for="dict in dict.type.moe_project_category" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option v-for="dict in dict.type.moe_project_level" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable v-model="form.finishTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择项目状态">
            <el-option v-for="dict in dict.type.moe_project_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMessage, delMessage, getMessage, listMessage, updateMessage, upDateReadMessage} from "@/api/system/message";
import {mapMutations} from 'vuex'

export default {
  name: "Message",
  dicts: ['moe_message_status', 'moe_message_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        senderId: [
          { required: true, message: "发送者不能为空", trigger: "change" }
        ],
        receiverId: [
          { required: true, message: "接收者不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类别不能为空", trigger: "change" }
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    ...mapMutations(['getCurrenTotal']),
    /** 查询消息列表 */
    getList () {
      this.loading = true;
      let userId = localStorage.getItem("userId")
      listMessage({ receiverId: userId }).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        senderId: null,
        senderName: null,
        receiverId: null,
        receiverName: null,
        title: null,
        content: null,
        type: null,
        readStatus: null,
        readTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 标记为已读
    signRead () {

      if (this.ids != null) {
        let userId = localStorage.getItem("userId")
        // 调用接口
        upDateReadMessage({ userId: userId, messageIds: this.ids }).then(response => {
          this.$message('消息已阅读');
          this.getList()
          EventBus.$emit('my-event')
        }
        )
      }
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加消息";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息";
      });
    },
    handleGetMessage (row) {
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "消息详情";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息编号为"' + ids + '"的数据项？').then(function () {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('system/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
